package com.base.controller.web;

import com.base.dto.MoneyGoldUpdateWebDTO;
import com.base.dto.MoneyGoldWebListDTO;
import com.base.exception.PtException;
import com.base.po.param.NetState;
import com.base.po.voip.MoneyGold;
import com.base.po.voip.Subscribers;
import com.base.service.MoneyGoldService;
import com.base.service.SubscribersService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;

@RestController
@Slf4j
@RequestMapping("/admin")
public class MoneyGoldWebController {
    private final MoneyGoldService moneyGoldService;
    private final SubscribersService subscribersService;

    public MoneyGoldWebController(MoneyGoldService moneyGoldService, SubscribersService subscribersService) {
        this.moneyGoldService = moneyGoldService;
        this.subscribersService = subscribersService;
    }

    @GetMapping(value = "/moneyGoldIndex")
    public ModelAndView moneyGoldIndex() {
        return new ModelAndView("money/index_goldMoney");
    }

    @PostMapping(value = "/moneyGoldWebList")
    @Validated
    public NetState moneyGoldWebList(@Valid final MoneyGoldWebListDTO moneyGoldWebListDTO) {
        return moneyGoldService.moneyGoldWebList(moneyGoldWebListDTO);
    }

    @PostMapping(value = "/moneyGoldWebAdd")
    public NetState moneyGoldWebAdd(@ApiParam("转账用户Id") @RequestParam String addMobile, @ApiParam("addStatus") @RequestParam Integer addStatus, @ApiParam("addMoney") @RequestParam BigDecimal addMoney, @ApiParam("addContext") @RequestParam String addContext) {
        Subscribers sub = Optional.ofNullable(subscribersService.selectBySubscribers(addMobile, null)).orElseThrow(() -> new PtException("新增用户为空"));
        if (StringUtils.isEmpty(addContext)) {
            addContext = "后台管理新增记录";
        }
        MoneyGold moneyGold = new MoneyGold(null, sub.getId(), sub.getId(), addContext, addMoney, addStatus, 1, new Date());
        moneyGoldService.insertSelective(moneyGold);
        return new NetState(NetState.SUCCESS_CONN, "记录新增成功");
    }

    @PostMapping(value = "/moneyGoldWebUpdate")
    @Validated
    public NetState moneyGoldWebUpdate(@Valid final MoneyGoldUpdateWebDTO moneyGoldUpdateWebDTO) {
        MoneyGold moneyGold = Optional.ofNullable(moneyGoldService.selectByPrimaryKey(moneyGoldUpdateWebDTO.getUpdateId())).orElseThrow(() -> new PtException("更新异常"));
        String userId=null;
        if (!StringUtils.isEmpty(moneyGoldUpdateWebDTO.getUpdateMobile())){
            userId=Optional.ofNullable(subscribersService.selectBySubscribers(moneyGoldUpdateWebDTO.getUpdateMobile(),null)).orElseThrow(()->new PtException("获得积分的用户不存在")).getId();
        }
        moneyGold.setGoldStatus(moneyGoldUpdateWebDTO.getUpdateStatus());
        moneyGold.setGoldTradeMoney(moneyGoldUpdateWebDTO.getUpdateMoney());
        moneyGold.setGoldUser(userId);
        moneyGoldService.updateByPrimaryKeySelective(moneyGold);
        return new NetState(NetState.SUCCESS_CONN, "更新记录成功");
    }

    @PostMapping(value = "/moneyGoldWebDelete")
    public NetState moneyGoldWebDelete(@ApiParam("id") @RequestParam Integer id) {
        return new NetState(NetState.SUCCESS_CONN, "删除成功", moneyGoldService.deleteByPrimaryKey(id));
    }
}
